$(function () {
    const $iptPhone     = $("#find-ipt-phone");   // 要查找的
    const $labelFind    = $("#find-label-phone"); //

    const $name          = $("#ipt-name");       // 查找到的内容
    const $passwd        = $("#ipt-passwd");     //
    const $email         = $("#ipt-email");      //
    const $phone         = $("#ipt-phone");      //
    const $idNumber      = $("#ipt-idNumber");   //
    const $dispatches    = $("#ipt-disp");       //
    const $status_0      = $("#status-0");       //
    const $status_2      = $("#status-2");       //
    const $regtime       = $("#ipt-regtime");    //
    const $logintime     = $("#ipt-logintime");  //
    const $input         = $("input[id|=ipt]");  //
    const $addr          = $("#addr");           //

    const $labelName        = $("#label-name");     // 标签
    const $labelPasswd      = $("#label-passwd");   //
    const $labelEmail       = $("#label-email");    //
    const $labelPhone       = $("#label-phone");    //
    const $labelIdNumber    = $("#label-idNumber"); //
    const $labelDisp        = $("#label-disp");     //
    const $labelAddr        = $("#label-addr");     //

    const $find           = $("#find");       // 按钮
    const $submit         = $("#submit");     //
    const $reset          = $("#reset");      //

    const $res = $("#res");
    const doc = {
        $iptPhone: $iptPhone,
        $labelFind: $labelFind,
        $name: $name,
        $passwd: $passwd,
        $email: $email,
        $phone: $phone,
        $idNumber: $idNumber,
        $dispatches: $dispatches,
        $status_0: $status_0,
        $status_2: $status_2,
        $regtime: $regtime,
        $logintime: $logintime,
        $addr: $addr,
        $labelName: $labelName,
        $labelPasswd: $labelPasswd,
        $labelEmail: $labelEmail,
        $labelPhone: $labelPhone,
        $labelIdNumber: $labelIdNumber,
        $labelDisp: $labelDisp,
        $labelAddr: $labelAddr,
        $find: $find,
        $submit: $submit,
        $reset: $reset,
        $res: $res
    }
    findCourier(doc);
    {
        $name.bind('input propertychange', function () {
            test_ipt(testRealName, $name, $labelName);})
        $passwd.bind('input propertychange', function () {
            test_ipt(testPasswd, $passwd, $labelPasswd);})
        $email.bind('input propertychange', function () {
            test_ipt(testEmail, $email, $labelEmail);})
        $phone.bind('input propertychange', function () {
            test_ipt(testPhone, $phone, $labelPhone);})
        $idNumber.bind('input propertychange', function () {
            test_ipt(testIdNumber, $idNumber, $labelIdNumber);})
        $dispatches.bind('input propertychange', function () {
            test_ipt(testDispatches, $dispatches, $labelDisp);})
    }
    let updateUser = function () {
        $name.val($name.val().trim());
        $email.val($email.val().trim());
        $phone.val($phone.val().trim());
        $idNumber.val($idNumber.val().trim());
        $dispatches.val($dispatches.val().trim());
        if ((json.name === $name.val()) &&
            (json.passwd === $passwd.val()) &&
            (json.email === $email.val()) &&
            (json.phone === $phone.val()) &&
            (json.addr === $addr.val()) &&
            (json.idNumber === $idNumber.val()) &&
            (json.dispatches === $dispatches.val()) &&
            (json.status === '正常' ? $status_0.prop('checked') : $status_2.prop('checked'))) {
            layer.msg('还没有做任何修改呢');
            return false;
        }
        if (!test_ipt(testRealName, $name, $labelName)) {
            layer.msg('请输入真实姓名');
            $name.focus();
            return false;
        }
        if (!test_ipt(testPasswd, $passwd, $labelPasswd)) {
            layer.msg('密码格式不正确');
            $passwd.focus();
            return false;
        }
        if (!test_ipt(testEmail, $email, $labelEmail)) {
            layer.msg('邮箱地址有问题');
            $email.focus();
            return false;
        }
        if (!test_ipt(testPhone, $phone, $labelPhone)) {
            layer.msg('请输入正确的手机号码');
            $phone.focus();
            return false;
        }
        if (!test_ipt(testIdNumber, $idNumber, $labelIdNumber)) {
            layer.msg('身份证号码有误');
            $idNumber.focus();
            return false;
        }
        if (!test_ipt(testDispatches, $dispatches, $labelDisp)) {
            layer.msg('派件数必须是一个非负整数');
            return false;
        } else if (parseInt($dispatches.val(), 10) > 0x7fffffff) {
            layer.msg('额。。派件数似乎有点太多了');
            return false;
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/courier/update.do", {
            id: json.id,
            name: $name.val(),
            passwd: $passwd.val(),
            email: $email.val(),
            phone: $phone.val(),
            idNumber: $idNumber.val(),
            dispatches: $dispatches.val(),
            addr: $addr.val(),
            status: $status_0.prop('checked') ? 0 : 2
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status > 0) {
                $name.val("");
                $passwd.val("");
                $email.val("");
                $phone.val("");
                $idNumber.val("");
                $dispatches.val("");
                $res.hide();
                json.waitingForSamePhone = false;
            } else if (data.status === -404) {
                location.assign('/login.html');
            } else {
                switch (data.status) {
                    case -2:
                        $name.addClass("input-error");
                        $labelName.addClass("input-error");
                        $name.focus();
                        break;
                    case -3:
                        $phone.addClass("input-error");
                        $labelPhone.addClass("input-error");
                        $phone.focus();
                }
            }
        });
        return false;
    };
    setEnterKeyEvent($input, updateUser);
    showTips($passwd, $labelPasswd);
    $submit.click(updateUser);
    $reset.click(function () {reset(doc)});
});